125B - Simple XML - CodeForces Solution


implementation *1000

Please click on ads to support us..

Python Code:

def solve():
    out = ""
    current_tag = ""
    tabs = 0
    for i in input():
        current_tag += i
        if i == ">":
            if current_tag[1] == "/":
                if tabs-1 < 0:
                    out += current_tag
                    out += '\n'
                    current_tag = ""
                else:
                    tabs -= 1
                    out += tabs*'  '
                    out += current_tag
                    out += '\n'
                    current_tag = ""
            else:
                out += tabs*'  '
                out += current_tag
                out += '\n'
                tabs += 1
                current_tag = ""

    print(out)


solve()

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define rew(x)  for(int i = 0 ; i < x ; i++)

int main(){
    string s;
    cin >> s;
    int n = s.length();
    string space = "  ";
    int spc = 0;
    int looping = (n/7)*6;
    int temp = looping;
    int i = 0;
    while(looping){
        char a = s[i];
        
        


        if(a == '/'){
            looping += 1;
            spc -=1;
        }
        if(((a - 'a') <26) && ((a - 'a') >= 0)){
            if(s[i + 4] == a){
                spc -= 1;
            }
        }
        if(((a - 'a') <26) && ((a - 'a') >= 0)){
            if((s[i + 4] != a  ) && (s[i+3] == '/')){
                spc -= 1;
            }
        }



        cout << a;



        if(a == '>'){
            cout << "\n";
            spc+=1;
            rew(spc){
            cout << space;
        }
        }












        looping--;
        i++;
    }
    
}


Comments

Submit
0 Comments
More Questions

1455C - Ping-pong
1644C - Increase Subarray Sums
1433A - Boring Apartments
1428B - Belted Rooms
519B - A and B and Compilation Errors
1152B - Neko Performs Cat Furrier Transform
1411A - In-game Chat
119A - Epic Game
703A - Mishka and Game
1504C - Balance the Bits
988A - Diverse Team
1312B - Bogosort
1616B - Mirror in the String
1660C - Get an Even String
489B - BerSU Ball
977C - Less or Equal
1505C - Fibonacci Words
1660A - Vasya and Coins
1660E - Matrix and Shifts
1293B - JOE is on TV
1584A - Mathematical Addition
1660B - Vlad and Candies
1472C - Long Jumps
1293D - Aroma's Search
918A - Eleven
1237A - Balanced Rating Changes
1616A - Integer Diversity
1627B - Not Sitting
1663C - Pōja Verdon
1497A - Meximization